package com.woniuxy.user.controller;

import com.woniuxy.user.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:阳佳
 * @Date: 2020/7/22 16:45
 * @Description:用户学习记录 点开某个课件 则记录一次学习记录
 * @PACKAGE_NAME:com.woniuxy.user.controller
 */
@Api("用户学习记录相关操作")
@RestController
@RequestMapping("/user")
public class UserStuHistoryController {
    @Autowired
    private RedisService redisService;

    @ApiOperation("增加一条学习记录缓存")
    @GetMapping("addCache/{uid}/{cwid}")
    public void addCache(@ApiParam("用户id") @PathVariable String uid, @ApiParam("课件id") @PathVariable String cwid) {
        String key = uid+cwid;
        //为了保证学习课件的 唯一性,每次添加前,将list 中该 课件ID去掉,在加入,以保证其学习的最新的课件在最前面
        redisService.lrem(key, 1, cwid);
        //将value push 到该key下的list中
        redisService.lpush(key, cwid);
        //使用ltrim将60个数据之后的数据剪切掉
        redisService.lTrim(key, 0, 59);
        //设置缓存时间为一个月
        redisService.expire(key, 60 * 60 * 24 * 30);
    }
}
